package com.bop.dehong.loan;

import java.util.ArrayList;
import java.util.List;

import com.base.dehong.http.RequestCallBack;
import com.base.dehong.util.ToastUtils;
import com.bop.dehong.R;
import com.bop.dehong.base.DeHongApp;
import com.bop.dehong.base.TopManager;
import com.bop.dehong.config.Config;
import com.bop.dehong.http.model.LoanModel;
import com.bop.dehong.info.TenderingListInfo;
import com.bop.dehong.login.LoginActivity;
import com.bop.dehong.main.BottomBarActivity;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;

/**
 * 我的借款
 * @author TangWenhan/YeFengyun
 *2015-11-18下午3:09:28
 */
public class MyLoanActivity extends BottomBarActivity{
	
	TenderingFragment mTenderingFragment;
	RepaymentFragment mRepaymentFragment;
	PayOffFragment mPayOffFragment;
	List<Fragment> mFragments;
	
	private LoanModel mLoadModel;
	private int mType = 0, mPage;

	@Override
	protected List<Fragment> getFragments() {
		mFragments = new ArrayList<Fragment>();
		mTenderingFragment = new TenderingFragment();
		mRepaymentFragment = new RepaymentFragment();
		mPayOffFragment = new PayOffFragment();
		mFragments.add(mTenderingFragment);
		mFragments.add(mRepaymentFragment);
		mFragments.add(mPayOffFragment);
		return mFragments;
	}

	@Override
	protected List<View> getBottomViews() {
		List<View> views = new ArrayList<View>();
		views.add(getView(R.id.tendering));
		views.add(getView(R.id.repayment));
		views.add(getView(R.id.payoff));
		return views;
	}
	@Override
	protected void initTop() {
		super.initTop();
		View mView = getView(R.id.top);
		TopManager manager = new TopManager(mView);
		manager.setTitle(R.string.wodejiekuan);
		manager.setLeftImageVisibility(View.VISIBLE);
		
	}
	public void reqData(int type,int page){
		if (mLoadModel == null) {
			mLoadModel = new LoanModel(this, reqCallback);
		}
		mType = type;
		mPage = page;
		String uid = DeHongApp.mLoginEntity.uid;
		if (TextUtils.isEmpty(uid))
		{
			ToastUtils.show(this, "你还未登录，请先登录！");
			Intent intent = new Intent(this, LoginActivity.class);
			intent.putExtra(Config.GO_BACK, true);
			startActivityForResult(intent, 100);
			return;
		}
		showProgressDialog();
		mLoadModel.doNet(uid, Integer.toString(type), Integer.toString(page));
	}
	RequestCallBack<TenderingListInfo> reqCallback = new RequestCallBack<TenderingListInfo>() {

		@Override
		public void end() {
			disappearProgressDialog();
		}

		@Override
		public void onSuccess(TenderingListInfo info) {
			super.onSuccess(info);
			((LoanBaseFragment)mFragments.get(mType)).initData(info);
		}
		
	};

	@Override
	protected void initFragments() {
	}

	@Override
	protected int getContentViewId() {
		return R.layout.act_my_loan;
	}

	@Override
	protected void onActivityResult(int arg0, int resultCode, Intent arg2) {
		super.onActivityResult(arg0, resultCode, arg2);
		if (resultCode != RESULT_OK) {
			finishCurrentActivity();
			return;
		}
		reqData(mType, mPage);
	}
}

